#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
	// 取数值各个位上的单数之和
	int getSum(int n) {
		int sum = 0;
		while (n) {
			sum += (n % 10) * (n % 10);
			n /= 10;
		}
		return sum;
	}
	bool isHappy(int n) {
		unordered_set<int> set;
		while(1) {
			int sum = getSum(n);
			if (sum == 1) {
				return true;
			}
			// 如果这个sum曾经出现过，说明已经陷入了无限循环了，立刻return false
			if (set.find(sum) != set.end()) {
				return false;
			} else {
				set.insert(sum);
			}
			n = sum;
		}
	}
};
class Test{
public:
	int getNum(int n){
		int sum = 0;
		while(n){
			int tmp = n % 10;
			sum += tmp * tmp;
			n /= 10;
		}
		return sum;
	}
	bool isHappy(int num){
		set<int> s;
		s.insert(num);
		while(num != 1){
			num = getNum(num);
			if(s.find(num)!=s.end()){
				return false;
			}else{
				s.insert(num);
			}
		}
		return true;
		
	}
};


int main(){
	Solution s;
	bool res = s.isHappy(2);
	if(res){
		cout << "true"<< endl;
	}else{
		cout << "false" << endl;
	}
	return 0;
}
